# Alexander F. Gazmararian
# afg2@princeton.edu
rm(list = ls())
gc()
# Load packages
library(tidyverse)
library(modelsummary)
# Load data
g <- readRDS("data/FairGazmararian_2021.rds")
# Make income ordered
g$income_acs <- gsub("[$]", "", g$income_acs)
g$income_acs <- factor(g$income_acs, ordered = TRUE, levels = c("Less than 20,000", "20,000 to 39,999", "40,000 to 59,999",
                                                                "60,000 - 99,999", "100,000 or more"))
g$Dem <- ifelse(g$pid == "Democrat", 1, 0)
# Get summary statistics
file <- "tables/tbl_summarystat_fair1.txt"
datasummary(
  formula = (Female = sex) + (White = white) + (College = college) + (`Fossil Fuel Employment` = ffemploy) + 
    (`Full-Time Employed` = fulltime) + (Democrat = Dem) + (Republican = Rep) +
    (`Climate Change Concern` = gw_binary) + (Income = income_acs) + (Age = age) ~ Mean + SD + N + Percent(),
  data = g,
  fmt = 2,
  escape = FALSE,
  output = "latex"
) %>%
  cat(., file = file)
# Trim table shell
tbl <- readLines(file, warn = FALSE)
tbl <- tbl[-c(1,2,28)]
cat(tbl, file = file)